package com.zhuss.excecise.processor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.excecise.excecise.wechat.DelTopicExceciseBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.excecise.db.dao.ext.ExceciseTopicDao;
import com.zhuss.util.JsonUtil;

/**
 * 删除评论练习生 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("exceciseDelTopic")
public class ExceciseDelTopic extends AbstractProcessor<Object, DelTopicExceciseBo> implements Processor<Object, DelTopicExceciseBo> {

    @Autowired
	@Qualifier("exceciseRead")
	private ExceciseRead exceciseRead;
    
    @Autowired
	@Qualifier("exceciseTopicRead")
	private ExceciseTopicRead exceciseTopicRead;
    
    @Autowired
    private ExceciseTopicDao exceciseTopicDao;
    
    /**
     * 校验
     */
    @Override
    public void check(DelTopicExceciseBo bo) throws WpsException {
    	
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(DelTopicExceciseBo param) throws WpsException {
    	Long exceciseTopicId = param.getExceciseTopicId();
    	List<Long> exceciseTopicIdList = param.getExceciseTopicIdList();
    	exceciseTopicIdList = new ArrayList<Long>();
    	exceciseTopicIdList.add(exceciseTopicId);
    	param.setExceciseTopicIdList(exceciseTopicIdList);
    	batchProcess(param);
    	param.setName(exceciseTopicId + "");
    	return null;
    }
    
    private void batchProcess(DelTopicExceciseBo param) throws WpsException {
    	List<Long> exceciseTopicIdList = param.getExceciseTopicIdList();
    	
    	if (exceciseTopicIdList == null || exceciseTopicIdList.isEmpty()) {
    		return;
    	}
    	
    	Integer deleted = param.getDeleted();
    	Long modifierId = param.getConcurrentUserId();
    	String modifier = param.getConcurrentUserName();
    	Date modifyTime = new Date();
    	
    	List<Long> exceciseTopicSonList = exceciseTopicRead.listSon(exceciseTopicIdList);
    	DelTopicExceciseBo sonBo = new DelTopicExceciseBo();
    	BeanUtils.copyProperties(param, sonBo);
    	if (exceciseTopicSonList != null && !exceciseTopicSonList.isEmpty()) {
    		sonBo.setExceciseTopicIdList(exceciseTopicSonList);
    	} else {
    		sonBo.setExceciseTopicIdList(null);
    	}
    	batchProcess(sonBo);
    	exceciseTopicDao.deleteByParentBatch(exceciseTopicIdList, deleted, modifierId, modifier, modifyTime);
    	exceciseTopicDao.deleteBatch(exceciseTopicIdList, deleted, modifierId, modifier, modifyTime);
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DelTopicExceciseBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.练习生.key());
    	record.setAction(LogActionEnum.练习生删除评论.key());
    	record.setTargetId(param.getExceciseTopicId());
    	record.setTargetName("评论内容：" + param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
